package com.wen.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.edu.entity.EduSubject;
import com.wen.edu.entity.excel.ExcelSubjectData;
import com.wen.edu.entity.subject.OneSubject;
import com.wen.edu.entity.subject.TwoSubject;
import com.wen.edu.listener.ExcelSubjectListener;
import com.wen.edu.mapper.EduSubjectMapper;
import com.wen.edu.service.EduSubjectService;
import com.wen.servicebase.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author wen
 * @since 2021-05-29
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 读取上传的Excel文件，添加课程分类
     *
     * @param file              Excel文件
     * @param eduSubjectService 实现文件读取时，监听器需要的参数
     */
    @Override
    public void addSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
//        获取文件输入流
            InputStream inputStream = file.getInputStream();
//        执行文件内容的读取
            EasyExcel.read(inputStream, ExcelSubjectData.class, new ExcelSubjectListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("课程分类Excel文件读取失败");
        }
    }

    /**
     * 查询课程列表并按照树形结构显示
     *
     * @return 所有的课程分类
     */
    @Override
    public List<OneSubject> getAllSubject() {
//        查询所有的一级分类课程
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneSubjects = this.list(wrapperOne);
//        封装一级分类和二级分类
//        声明一个list集合用来返回数据
        List<OneSubject> responseSubject = new ArrayList<>();
//        遍历得到的一级分类课程
        for (EduSubject oneSubject : oneSubjects) {
//            创建一个新的一级分类对象
            OneSubject firstSubject = new OneSubject();
//            将遍历的每个一级分类对象的属性复制到新的一级分类对象中
            BeanUtils.copyProperties(oneSubject, firstSubject);
//            查询条件
            QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
            wrapperTwo.eq("parent_id", oneSubject.getId());
            List<EduSubject> twoSubjects = this.list(wrapperTwo);
//            secondSubjects用来封装查询到的一级分类对应的二级分类
//            遍历给secondSubjects添加属性
            List<TwoSubject> secondSubjects = new ArrayList<>();
            for (EduSubject twoSubject : twoSubjects) {
                TwoSubject secondSubject = new TwoSubject();
                BeanUtils.copyProperties(twoSubject, secondSubject);
                secondSubjects.add(secondSubject);
            }
//            firstSubject对象中还有相应的二级分类属性children，根据每个一级分类的id查询相应的二级分类
            firstSubject.setChildren(secondSubjects);
            responseSubject.add(firstSubject);
        }
        return responseSubject;
    }
}
